{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Algorithmic Trading Cookbook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 2: Stock Markets: Primer on Trading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This Jupyter Notebook is created using Python version 3.8.2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Requirements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can install the requirements for this Jupyter Notebook by executing the below cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pyalgotrading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 1: Setup Python connectivity with Broker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installing package kiteconnect via pip...\n",
      "Please login to this link to generate your request token: https://kite.trade/connect/login?api_key=<your-api-key>&v=3\n"
     ]
    }
   ],
   "source": [
    "# Get the api_key & api_secret from broker. These are unique to you & will be used by the broker to identify your demat account.\n",
    "api_key = \"<your-api-key>\"\n",
    "api_secret = \"<your-api-secret>\"\n",
    "broker_connection = BrokerConnectionZerodha(api_key, api_secret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the request token from the above URL\n",
    "request_token = \"<your-request-token>\"\n",
    "broker_connection.set_access_token(request_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 2: Query list of Instruments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instrument_token</th>\n",
       "      <th>exchange_token</th>\n",
       "      <th>tradingsymbol</th>\n",
       "      <th>name</th>\n",
       "      <th>last_price</th>\n",
       "      <th>expiry</th>\n",
       "      <th>strike</th>\n",
       "      <th>tick_size</th>\n",
       "      <th>lot_size</th>\n",
       "      <th>instrument_type</th>\n",
       "      <th>segment</th>\n",
       "      <th>exchange</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>540584966</td>\n",
       "      <td>2111660</td>\n",
       "      <td>EURINR20DEC77.7500CE</td>\n",
       "      <td>EURINR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020-12-29</td>\n",
       "      <td>77.75</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>1</td>\n",
       "      <td>CE</td>\n",
       "      <td>BCD-OPT</td>\n",
       "      <td>BCD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>540578822</td>\n",
       "      <td>2111636</td>\n",
       "      <td>EURINR20DEC77.7500PE</td>\n",
       "      <td>EURINR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020-12-29</td>\n",
       "      <td>77.75</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>1</td>\n",
       "      <td>PE</td>\n",
       "      <td>BCD-OPT</td>\n",
       "      <td>BCD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>540512774</td>\n",
       "      <td>2111378</td>\n",
       "      <td>EURINR20DEC78.0000CE</td>\n",
       "      <td>EURINR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020-12-29</td>\n",
       "      <td>78.00</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>1</td>\n",
       "      <td>CE</td>\n",
       "      <td>BCD-OPT</td>\n",
       "      <td>BCD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>540506374</td>\n",
       "      <td>2111353</td>\n",
       "      <td>EURINR20DEC78.0000PE</td>\n",
       "      <td>EURINR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020-12-29</td>\n",
       "      <td>78.00</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>1</td>\n",
       "      <td>PE</td>\n",
       "      <td>BCD-OPT</td>\n",
       "      <td>BCD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>540627462</td>\n",
       "      <td>2111826</td>\n",
       "      <td>EURINR20DEC78.2500CE</td>\n",
       "      <td>EURINR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020-12-29</td>\n",
       "      <td>78.25</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>1</td>\n",
       "      <td>CE</td>\n",
       "      <td>BCD-OPT</td>\n",
       "      <td>BCD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65392</th>\n",
       "      <td>978945</td>\n",
       "      <td>3824</td>\n",
       "      <td>ZODJRDMKJ</td>\n",
       "      <td>ZODIAC JRD-MKJ</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0500</td>\n",
       "      <td>1</td>\n",
       "      <td>EQ</td>\n",
       "      <td>NSE</td>\n",
       "      <td>NSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65393</th>\n",
       "      <td>2916865</td>\n",
       "      <td>11394</td>\n",
       "      <td>ZOTA</td>\n",
       "      <td>ZOTA HEALTH CARE</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0500</td>\n",
       "      <td>1</td>\n",
       "      <td>EQ</td>\n",
       "      <td>NSE</td>\n",
       "      <td>NSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65394</th>\n",
       "      <td>7437825</td>\n",
       "      <td>29054</td>\n",
       "      <td>ZUARI-BE</td>\n",
       "      <td>ZUARI AGRO CHEMICALS</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0500</td>\n",
       "      <td>1</td>\n",
       "      <td>EQ</td>\n",
       "      <td>NSE</td>\n",
       "      <td>NSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65395</th>\n",
       "      <td>979713</td>\n",
       "      <td>3827</td>\n",
       "      <td>ZUARIGLOB</td>\n",
       "      <td>ZUARI GLOBAL</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0500</td>\n",
       "      <td>1</td>\n",
       "      <td>EQ</td>\n",
       "      <td>NSE</td>\n",
       "      <td>NSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65396</th>\n",
       "      <td>4514561</td>\n",
       "      <td>17635</td>\n",
       "      <td>ZYDUSWELL</td>\n",
       "      <td>ZYDUS WELLNESS</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0500</td>\n",
       "      <td>1</td>\n",
       "      <td>EQ</td>\n",
       "      <td>NSE</td>\n",
       "      <td>NSE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>65397 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       instrument_token exchange_token         tradingsymbol  \\\n",
       "0             540584966        2111660  EURINR20DEC77.7500CE   \n",
       "1             540578822        2111636  EURINR20DEC77.7500PE   \n",
       "2             540512774        2111378  EURINR20DEC78.0000CE   \n",
       "3             540506374        2111353  EURINR20DEC78.0000PE   \n",
       "4             540627462        2111826  EURINR20DEC78.2500CE   \n",
       "...                 ...            ...                   ...   \n",
       "65392            978945           3824             ZODJRDMKJ   \n",
       "65393           2916865          11394                  ZOTA   \n",
       "65394           7437825          29054              ZUARI-BE   \n",
       "65395            979713           3827             ZUARIGLOB   \n",
       "65396           4514561          17635             ZYDUSWELL   \n",
       "\n",
       "                       name  last_price      expiry  strike  tick_size  \\\n",
       "0                    EURINR         0.0  2020-12-29   77.75     0.0025   \n",
       "1                    EURINR         0.0  2020-12-29   77.75     0.0025   \n",
       "2                    EURINR         0.0  2020-12-29   78.00     0.0025   \n",
       "3                    EURINR         0.0  2020-12-29   78.00     0.0025   \n",
       "4                    EURINR         0.0  2020-12-29   78.25     0.0025   \n",
       "...                     ...         ...         ...     ...        ...   \n",
       "65392        ZODIAC JRD-MKJ         0.0                0.00     0.0500   \n",
       "65393      ZOTA HEALTH CARE         0.0                0.00     0.0500   \n",
       "65394  ZUARI AGRO CHEMICALS         0.0                0.00     0.0500   \n",
       "65395          ZUARI GLOBAL         0.0                0.00     0.0500   \n",
       "65396        ZYDUS WELLNESS         0.0                0.00     0.0500   \n",
       "\n",
       "       lot_size instrument_type  segment exchange  \n",
       "0             1              CE  BCD-OPT      BCD  \n",
       "1             1              PE  BCD-OPT      BCD  \n",
       "2             1              CE  BCD-OPT      BCD  \n",
       "3             1              PE  BCD-OPT      BCD  \n",
       "4             1              CE  BCD-OPT      BCD  \n",
       "...         ...             ...      ...      ...  \n",
       "65392         1              EQ      NSE      NSE  \n",
       "65393         1              EQ      NSE      NSE  \n",
       "65394         1              EQ      NSE      NSE  \n",
       "65395         1              EQ      NSE      NSE  \n",
       "65396         1              EQ      NSE      NSE  \n",
       "\n",
       "[65397 rows x 12 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "instruments = broker_connection.get_all_instruments()\n",
    "instruments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total instruments: 65397\n"
     ]
    }
   ],
   "source": [
    "print(f'Total instruments: {len(instruments)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 3: Fetch an Instrument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "segment: NSE\n",
       "exchange: NSE\n",
       "tradingsymbol: TATASTEEL\n",
       "broker_token: 895745\n",
       "tick_size: 0.05\n",
       "lot_size: 1\n",
       "expiry: \n",
       "strike_price: 0.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "broker_connection.get_instrument(segment='NSE', tradingsymbol='TATASTEEL')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 4: Query list of Exchanges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['BCD' 'BSE' 'NSE' 'CDS' 'MCX' 'NFO']\n"
     ]
    }
   ],
   "source": [
    "exchanges = instruments.exchange.unique()\n",
    "print(exchanges)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 5: Query list of Segments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['BCD-OPT' 'BCD-FUT' 'BCD' 'BSE' 'INDICES' 'CDS-OPT' 'CDS-FUT' 'MCX-FUT'\n",
      " 'MCX-OPT' 'NFO-OPT' 'NFO-FUT' 'NSE']\n"
     ]
    }
   ],
   "source": [
    "segments = instruments.segment.unique()\n",
    "print(segments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 6: Query support for Order Transaction Types, Order Types, Order Codes & Order Varieties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<BrokerOrderTransactionTypeConstants.BUY: 'BUY'>,\n",
       " <BrokerOrderTransactionTypeConstants.SELL: 'SELL'>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(BrokerConnectionZerodha.ORDER_TRANSACTION_TYPE_MAP.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<BrokerOrderVarietyConstants.MARKET: 'ORDER_VARIETY_MARKET'>,\n",
       " <BrokerOrderVarietyConstants.LIMIT: 'ORDER_VARIETY_LIMIT'>,\n",
       " <BrokerOrderVarietyConstants.STOPLOSS_LIMIT: 'ORDER_VARIETY_STOPLOSS_LIMIT'>,\n",
       " <BrokerOrderVarietyConstants.STOPLOSS_MARKET: 'ORDER_VARIETY_STOPLOSS_MARKET'>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(BrokerConnectionZerodha.ORDER_VARIETY_MAP.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<BrokerOrderTypeConstants.REGULAR: 'ORDER_TYPE_REGULAR'>,\n",
       " <BrokerOrderTypeConstants.BRACKET: 'ORDER_TYPE_BRACKET'>,\n",
       " <BrokerOrderTypeConstants.COVER: 'ORDER_TYPE_COVER'>,\n",
       " <BrokerOrderTypeConstants.AMO: 'ORDER_TYPE_AFTER_MARKET_ORDER'>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(BrokerConnectionZerodha.ORDER_TYPE_MAP.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<BrokerOrderCodeConstants.INTRADAY: 'ORDER_CODE_INTRADAY'>,\n",
       " <BrokerOrderCodeConstants.DELIVERY: 'ORDER_CODE_DELIVERY_T0'>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(BrokerConnectionZerodha.ORDER_CODE_MAP.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 7: Place a simple REGULAR order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.constants import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "instrument = broker_connection.get_instrument(segment='NSE', tradingsymbol='TATASTEEL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "191209000001676\n"
     ]
    }
   ],
   "source": [
    "# Place a Simple Regular order - Buy, Regular, Intraday, Market order\n",
    "order_id = broker_connection.place_order(instrument=instrument, \n",
    "                                         order_transaction_type=BrokerOrderTransactionTypeConstants.BUY, \n",
    "                                         order_type=BrokerOrderTypeConstants.REGULAR, \n",
    "                                         order_code=BrokerOrderCodeConstants.INTRADAY, \n",
    "                                         order_variety=BrokerOrderVarietyConstants.MARKET, \n",
    "                                         quantity=1)\n",
    "order_id\n",
    "\n",
    "# Check your broker site & you will see an order placed with these parameters & order_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 8: Place a simple BRACKET Order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.constants import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "instrument = broker_connection.get_instrument(segment='NSE', tradingsymbol='ICICIBANK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "191212001268839\n"
     ]
    }
   ],
   "source": [
    "# Place a simple BRACKET order - Buy, Bracket, Intraday, Limit order\n",
    "ltp = broker_connection.get_ltp(instrument)\n",
    "order_id = broker_connection.place_order(instrument=instrument,\n",
    "                                         order_transaction_type=BrokerOrderTransactionTypeConstants.BUY,\n",
    "                                         order_type=BrokerOrderTypeConstants.BRACKET,\n",
    "                                         order_code=BrokerOrderCodeConstants.INTRADAY,\n",
    "                                         order_variety=BrokerOrderVarietyConstants.LIMIT,\n",
    "                                         quantity=1,\n",
    "                                         price=ltp-1,\n",
    "                                         stoploss=2,\n",
    "                                         target=2)\n",
    "order_id\n",
    "\n",
    "# Check your broker site & you will see an order placed with these parameters & order_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 9: Place a simple DELIVERY order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.constants import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "instrument = broker_connection.get_instrument(segment='NSE', tradingsymbol='AXISBANK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "191212001268956\n"
     ]
    }
   ],
   "source": [
    "# Place a simple DELIVERY order - Sell, Regular, Delivery, Market order\n",
    "order_id = broker_connection.place_order(instrument=instrument,\n",
    "                                         order_transaction_type=BrokerOrderTransactionTypeConstants.SELL,\n",
    "                                         order_type=BrokerOrderTypeConstants.REGULAR,\n",
    "                                         order_code=BrokerOrderCodeConstants.DELIVERY,\n",
    "                                         order_variety=BrokerOrderVarietyConstants.MARKET,\n",
    "                                         quantity=1)\n",
    "order_id\n",
    "\n",
    "# Check your broker site & you will see an order placed with these parameters & order_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 10: Place a simple INTRADAY order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalgotrading.constants import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "instrument = broker_connection.get_instrument(segment='NSE', tradingsymbol='HDFCBANK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "191212001269042\n"
     ]
    }
   ],
   "source": [
    "# Place a simple INTRADAY order - Sell, Bracket, Intraday, Limit order\n",
    "ltp = broker_connection.get_ltp(instrument)\n",
    "order_id = broker_connection.place_order(instrument=instrument,\n",
    "                                         order_transaction_type=BrokerOrderTransactionTypeConstants.SELL,\n",
    "                                         order_type=BrokerOrderTypeConstants.BRACKET,\n",
    "                                         order_code=BrokerOrderCodeConstants.INTRADAY,\n",
    "                                         order_variety=BrokerOrderVarietyConstants.LIMIT,\n",
    "                                         quantity=1,\n",
    "                                         price=ltp+1,\n",
    "                                         stoploss=2,\n",
    "                                         target=2)\n",
    "order_id\n",
    "\n",
    "# Check you broker site & you will see an order placed with these parameters & order_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 11: Query Margins & Funds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'enabled': True,\n",
       " 'net': 1623.67,\n",
       " 'available': {'adhoc_margin': 0,\n",
       "  'cash': 1623.67,\n",
       "  'opening_balance': 1623.67,\n",
       "  'live_balance': 1623.67,\n",
       "  'collateral': 0,\n",
       "  'intraday_payin': 0},\n",
       " 'utilised': {'debits': 0,\n",
       "  'exposure': 0,\n",
       "  'm2m_realised': 0,\n",
       "  'm2m_unrealised': 0,\n",
       "  'option_premium': 0,\n",
       "  'payout': 0,\n",
       "  'span': 0,\n",
       "  'holding_sales': 0,\n",
       "  'turnover': 0,\n",
       "  'liquid_collateral': 0,\n",
       "  'stock_collateral': 0}}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "equity_margins = broker_connection.get_margins('equity')\n",
    "equity_margins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1623.67"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "equity_funds = broker_connection.get_funds('equity')\n",
    "equity_funds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'enabled': True,\n",
       " 'net': 0,\n",
       " 'available': {'adhoc_margin': 0,\n",
       "  'cash': 0,\n",
       "  'opening_balance': 0,\n",
       "  'live_balance': 0,\n",
       "  'collateral': 0,\n",
       "  'intraday_payin': 0},\n",
       " 'utilised': {'debits': 0,\n",
       "  'exposure': 0,\n",
       "  'm2m_realised': 0,\n",
       "  'm2m_unrealised': 0,\n",
       "  'option_premium': 0,\n",
       "  'payout': 0,\n",
       "  'span': 0,\n",
       "  'holding_sales': 0,\n",
       "  'turnover': 0,\n",
       "  'liquid_collateral': 0,\n",
       "  'stock_collateral': 0}}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "commodity_margins = broker_connection.get_margins('commodity')\n",
    "commodity_margins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "commodity_funds = broker_connection.get_funds('commodity')\n",
    "commodity_funds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 12: Calculate Brokerage charged"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Brokerage charged per trade: 0.1245\n"
     ]
    }
   ],
   "source": [
    "entry_price = 1245\n",
    "brokerage = (0.01 * 1245)/100\n",
    "print(f'Brokerage charged per trade: {brokerage:.4f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Brokerage charged for 10 trades: 1.2450\n"
     ]
    }
   ],
   "source": [
    "# Assuming, 10 trades were placed in a day with an average price of 1245, we calculate the total brokerage involved\n",
    "total_brokerage = 10 * (0.01 * 1245) / 100\n",
    "print(f'Total Brokerage charged for 10 trades: {total_brokerage:.4f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recipe 13: Calculate Government taxes charged"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Government taxes charged per trade: 1.2450\n"
     ]
    }
   ],
   "source": [
    "entry_price = 1245\n",
    "brokerage = (0.1 * 1245)/100\n",
    "print(f'Government taxes charged per trade: {brokerage:.4f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Government taxes charged for 10 trades: 12.4500\n"
     ]
    }
   ],
   "source": [
    "# Assuming, 10 trades were placed in a day with an average price of 1245, we calculate the total brokerage involved\n",
    "total_brokerage = 10 * (0.1 * 1245) / 100\n",
    "print(f'Total Government taxes charged for 10 trades: {total_brokerage:.4f}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
